<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    /*重新设置样式*/
    .form-control {
        width: 196px !important;
        height: 34px !important;
        padding: 10px !important;
    }
</style>
<body class="hold-transition skin-blue sidebar-mini" onload="checkBrowser()">


<section class="content-header">
    <h1 class="pull-left" style="display: inline-block;line-height: 34px;">盘点损溢调整</h1>


    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a th:if="${checkPlanModel.cpnAdjStatus==3}"
           th:href="@{'/purchase/check/plan/dispatch/pal/'+${checkPlanModel.cpnId}}"
           shiro:hasPermission="purinv:purcheckadj:edit" id="btn-next"
           class="btn btn-warning">下一步
        </a>
    </div>
    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a href="/purchase/check" shiro:hasPermission="purinv:purcheckplan:view" id="btn-return-checkPlanList"
           class="btn btn-info">返回计划列表
        </a>
    </div>
    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a th:href="@{'/purchase/check/plan/dispatch/result/'+${checkPlanModel.cpnId}}"
           shiro:hasPermission="purinv:purcheckadj:edit" id="btn-prev"
           class="btn btn-success">上一步
        </a>
    </div>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header" style="padding-bottom: 0px">
                    <form id="queryForm">
                        <div class="row">
                            <div class="form-inline">
                                <div hidden class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right">
                                        计划编号
                                    </label>

                                        <input name="cpnCode" type="text" class="form-control text" maxlength="255"
                                               th:value="${checkPlanModel.cpnCode}" readonly/>

                                </div>
                                <div hidden class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right">
                                        盘点内容
                                    </label>

                                        <input type="text" class="form-control text"
                                               th:value="${checkPlanModel.cpnContent}"
                                               readonly="readonly"/>

                                </div>
                                <div class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right">
                                        项目编号
                                    </label>

                                    <input name="cdlMatProCode" type="text" class="form-control text"
                                           placeholder="请输入项目编号"/>

                                </div>
                                <div class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right">
                                        项目名称
                                    </label>

                                    <input name="cdlMatProName" type="text" class="form-control text"
                                           placeholder="请输入项目名称"/>

                                </div>
                                <div class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right">
                                        物料描述
                                    </label>

                                        <input name="cdlMatDesc" type="text" class="form-control text" maxlength="255"
                                               placeholder="请输入物料描述"/>

                                </div>



                                <div class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right;width: 56px;">
                                        区位
                                    </label>

                                        <input name="cdlMatPosition" type="text" class="form-control text"
                                               placeholder="请输入区位"/>

                                </div>
                                <div class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right">
                                        损溢状态
                                    </label>

                                        <select name="cdlCheckStatus"
                                                style="padding: 0px!important;text-align: center!important;text-align-last: center!important;width: 100px!important;"
                                                class="form-control select">
                                            <option value="">全部</option>
                                            <option value="1">盘盈</option>
                                            <option value="2">盘亏</option>
                                        </select>

                                </div>
                                <div class="form-group" style="margin-right: 10px;margin-bottom: 5px">
                                    <label class="control-label" style="text-align: right;width: 56px;">
                                        仓库
                                    </label>

                                        <select name="cdlMatWhiId"
                                                type="text" class="form-control select"
                                                style="padding: 0px!important;text-align: center!important;text-align-last: center!important;width: 100px!important;"
                                        >
                                        </select>

                                </div>
                                <div class="form-group"
                                     style="margin-right: 10px;margin-bottom: 5px">
                                    <button type="button" class="btn btn-success" id="btn-search">搜索
                                    </button>

                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="row" id="toolbar">
<!--                    <a shiro:hasPermission="purinv:purcheckadj:edit"  style="margin-right: 10px;margin-bottom: 5px"-->
<!--                       th:href="@{/purchase/check/adjust/export/{cpnId}(cpnId=${checkPlanModel.cpnId})}"-->
<!--                       id="btn_downCheckPlan" class="btn btn-primary">-->
<!--                        导出-->
<!--                    </a>-->
                    <button shiro:hasPermission="purinv:purcheckadj:edit"  style="margin-right: 10px;margin-bottom: 5px"
                            th:if="${checkPlanModel.cpnAdjStatus!=3}" id="btn_submit" type="button"
                            class="btn btn-success pull-right">提交盘点损溢调整
                    </button>
                    <button shiro:hasPermission="purinv:purcheckadj:edit"  style="margin-right: 10px;margin-bottom: 5px"
                            th:if="${checkPlanModel.cpnAdjStatus==3}" id="btn_revoke" type="button"
                            class="btn btn-primary pull-right">撤销
                    </button>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table" class="table table-bordered " width="100%">
                    </table>
                    <table id="check-progress-table" hidden class="table table-bordered " width="100%">
                        <thead>
                        <tr>
                            <th>
                                计划编号
                            </th>
                            <th>
                                盘点计划
                            </th>
                            <th>
                                盘点结果
                            </th>
                            <th>
                                盘点损溢调整
                            </th>
                            <th>
                                损溢表
                            </th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr id="check-progress-table-tbody-firstTr"></tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</section>

<!--盘点模态框-->
<div class="modal fade" id="modal-adjust">
    <div class="modal-dialog" style="width:500px;">
        <div class="modal-content">
            <div class="modal-header" style="padding-bottom: 0px;">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">调整</h4>
            </div>
            <div class="modal-body" style="padding-bottom: 5px;padding-top: 0px;">
                <div class="row">
                    <div class="col-xs-12">
                        <div class="box">
                            <div class="box-header">
                            </div>
                            <div class="box-body">
                                <form id="adjustModalForm" class="form-horizontal" role="form">
                                    <div hidden>
                                        <input type="hidden" name="cdlId">
                                    </div>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group" style="height: 60px;">
                                                <label class="form-label col-sm-3" for="cdlPalVal"
                                                       style="margin: 0px;padding: 0px;line-height: 35px ">损溢差</label>
                                                <div class="col-sm-9"
                                                     style="margin: 0px;padding: 0px;line-height: 35px;width: 120px;">
                                                    <input type="text" class="form-control"
                                                           style="border-width: 1px; border-radius:5px;border-color:#d2d6de;width:205px;font-size:12px;height:32px;padding: 6px 12px 6px 12px;text-align: center;vertical-align: middle"
                                                           oninput="xyh.inputDecimalThreeHaveNegative(this)"
                                                           id="cdlPalVal" name="cdlPalVal" placeholder="请输入损溢差"/>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group" style="height: 160px">
                                                <label class="form-label col-sm-3" for="cdlRemark"
                                                       style="margin: 0px;padding: 0px;line-height: 35px">
                                                    调整原因
                                                </label>
                                                <div class="col-sm-9"
                                                     style="margin: 0px;padding: 0px;">
                                                    <input type="text" id="cdlRemark" name="cdlRemark"

                                                           class="form-control" maxlength="255"
                                                           style="width: 200px"
                                                           placeholder="请输入备注">
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer" style="padding-top: 5px;">
                <button type="button" class="btn btn-info" id="btn-save-adjust">确定</button>
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>

</div>
<div th:include="include :: footer"></div>
<script th:src="@{/plugin/moment-jquery-H5-beautify-date/js/moment.min.js}"></script>
<script th:src="@{/plugin/moment-jquery-H5-beautify-date/js/zh-cn.js}"></script>
<script th:src="@{/plugin/moment-jquery-H5-beautify-date/js/es6.js}"></script>
<script th:inline="javascript">
    var table;


    function checkBrowserFun() {
        let is360 = false;
        let isIE = false;
        let isFirefox = false;
        let isChrome = false;
        let isEdge = false;
        let broName = 'Runing';
        let str = '';
        let strStart = 0;
        let strStop = 0;
        let arr = new Array();
        let temp = '';
        let userAgent = window.navigator.userAgent; //包含以下属性中所有或一部分的字符串：appCodeName,appName,appVersion,language,platform
        /*alert(userAgent);*/
        //FireFox
        if (userAgent.indexOf('Firefox') != -1) {
            isFireFox = true;
            /*broName = 'FireFox浏览器';*/
            strStart = userAgent.indexOf('Firefox');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号')

        }
        //Edge
        if (userAgent.indexOf('Edge') != -1) {
            isEdge = true;
            /*broName = 'Edge浏览器';*/
            strStart = userAgent.indexOf('Edge');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号');
        }
        //IE浏览器
        if (userAgent.indexOf('NET') != -1 && userAgent.indexOf("rv") != -1) {
            isIE = true;
            /*broName = 'IE浏览器'; */
            strStart = userAgent.indexOf('rv');
            strStop = userAgent.indexOf(')');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('rv', 'IE').replace(':', '版本号');
        }
        //360极速模式可以区分360安全浏览器和360极速浏览器
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") < 0 && userAgent.indexOf("Firefox") < 0) {
            if (navigator.javaEnabled()) {
                is360 = true;
                broName = '360安全浏览器-极速模式';
            } else {
                is360 = true;
                broName = '360极速浏览器-极速模式';
            }
        }
        //360兼容
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") != -1 && userAgent.indexOf("MSIE") != -1 && userAgent.indexOf("rv") < 0) {
            is360 = true;
            broName = '360兼容模式';
        }
        //Chrome浏览器
        if (userAgent.indexOf('WOW') < 0 && userAgent.indexOf('Chrome') > 0 && userAgent.indexOf("Edg") < 0) {
            isChrome = true;
            /*broName = 'Chrome浏览器';*/
            strStart = userAgent.indexOf('Chrome');
            strStop = userAgent.indexOf(' Safari');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('/', '版本号');
        }
        return isChrome;
    }

    /**
     * 检测浏览器
     * */
    function checkBrowser(){
        if(!checkBrowserFun()){
            alert("尊敬的客户，您使用浏览器不是谷歌浏览器，请使用谷歌浏览器访问本系统！");
            location.href=ctx+"overview/index";
        }
    }
    /**
     *@FunctionName: findWarehouseList
     *@Description: 获取仓库列表
     *@Author: 徐一贺
     *@CreateDate: 2020/3/24 15:31
     *@Phone: 18241927380
     *@Version: 1.0.0
     */
    function findWarehouseList() {
        js.ajax({
            url: ctx + 'common/warehouseInfo/list',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {},
            async: true,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    $("#queryForm [name='cdlMatWhiId']").empty();
                    $("#queryForm [name='cdlMatWhiId']").append('<option value="">全部</option>');
                    $("#modalForm [name='cdlWhiId']").empty();
                    $("#modalForm [name='cdlWhiId']").append('<option value="">全部</option>');
                    for (var i = 0; i < result.data.rows.length; i++) {
                        $("#queryForm [name='cdlMatWhiId']").append('<option value="' + result.data.rows[i].whiId + '">' + result.data.rows[i].whiName + '</option>');
                        $("#modalForm [name='cdlWhiId']").append('<option value="' + result.data.rows[i].whiId + '">' + result.data.rows[i].whiName + '</option>');
                    }
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        })
    }

    $(function () {
        $("title").html($("title").html()+" —— 盘点损益调整");
        findWarehouseList();
        var columnsOption = new Array();
        var mode = [[${checkPlanModel.cpnAdjStatus}]];
        if (mode == 3) {
            columnsOption = [
                {
                    title: '序号', field: 'cdlId', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '区位', field: 'cdlPosition'},
                {title: '物料描述', field: 'cdlMatDesc'},
                {title: 'SAP物料编码', field: 'cdlMatCode'},
                {title: '项目号', field: 'cdlProCode', formatter: function (value, row, index) {
                        if (value == undefined || value == null) {
                            value = "--";
                        }
                        return value;
                    }},
                {title: '项目名称', field: 'cdlProName', formatter: function (value, row, index) {
                        if (value == undefined || value == null) {
                            value = "--";
                        }
                        return value;
                    }},
                {title: '当前数量', field: 'cdlItyNum'},
                {title: '单位', field: 'cdlUnit', width: '50'},
                {title: '盘点数量', field: 'cdlNum'},
                {
                    title: '损溢差', field: 'cdlPalVal', formatter: function (value, row, index) {
                        return value;
                    },
                    cellStyle: function (value, row, index) {

                        if (value != undefined && value != null) {
                            if (value > 0) {
                                return {css: {"color": "green"}}
                            } else if (value < 0) {
                                return {css: {"color": "red", "font-weight": "900"}}
                            } else {
                                return {css: {"color": "#333333"}}
                            }
                        } else {
                            return {css: {"color": "#333333"}}
                        }
                    }
                },
                {
                    title: '调整原因', field: 'cdlRemark', formatter: function (value, row, index) {
                        return value;
                    }
                }
            ];
        } else {
            columnsOption = [
                {
                    title: '序号', field: 'cdlId', width: '50',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '区位', field: 'cdlPosition'},
                {title: '物料描述', field: 'cdlMatDesc'},
                {title: 'SAP物料编码', field: 'cdlMatCode'},
                {title: '项目号', field: 'cdlProCode', formatter: function (value, row, index) {
                        if (value == undefined || value == null) {
                            value = "--";
                        }
                        return value;
                    }},
                {title: '项目名称', field: 'cdlProName', formatter: function (value, row, index) {
                        if (value == undefined || value == null) {
                            value = "--";
                        }
                        return value;
                    }},
                {title: '当前数量', field: 'cdlItyNum'},
                {title: '单位', field: 'cdlUnit', width: '50'},
                {title: '盘点数量', field: 'cdlNum'},
                {
                    title: '损溢差', field: 'cdlPalVal', formatter: function (value, row, index) {
                        return value;
                    },
                    cellStyle: function (value, row, index) {

                        if (value != undefined && value != null) {
                            if (value > 0) {
                                return {css: {"color": "green"}}
                            } else if (value < 0) {
                                return {css: {"color": "red", "font-weight": "900"}}
                            } else {
                                return {css: {"color": "#333333"}}
                            }
                        } else {
                            return {css: {"color": "#333333"}}
                        }
                    }
                },
                {
                    title: '调整原因', field: 'cdlRemark', formatter: function (value, row, index) {
                        return value;
                    }
                },
                {
                    title: '操作', field: 'cpnId', width: '90',
                    formatter: function (value, row, index) {
                        var htmlText = "";
                        if (hasP('purinv:purcheckadj:edit')) {
                            htmlText += '<button style="margin-top: 5px;margin-left: 5px;width: 60px" class="btn btn-warning btn-xs" onclick="adjust(' + row.cdlId + ')">调整</button>';
                        }
                        return htmlText;
                    }
                }
            ];
        }

        //加载列表数据
        table = js.table.init({
            id: 'bootstrap-table',
            url: ctx + "purchase/check/result/list",
            showColumns: true,
            showExport: false,
            pageSize: 300,
            striped: false,
            uniqueId: "cdlId",
            onLoadSuccess: function () {
                $('#bootstrap-table thead th').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
                $('#bootstrap-table tbody tr:first td').each(function () {
                    $(this).width(Math.floor($(this).width()));
                });
            },
            columns: columnsOption
        });
        formValidator();
        /**
         * 表格搜索
         */
        $("#btn-search").on('click', function () {
            js.table.search(table);
        });
        /**
         * 调整模态框确认
         */
        $("#btn-save-adjust").on('click', function () {//销毁表单验证
            $("#adjustModalForm").data('bootstrapValidator').destroy();
            //置空表单验证
            $('#adjustModalForm').data('bootstrapValidator', null);
            //重新绑定表单验证
            formValidator();

            //验证通过式提交
            js.validSubmit({
                formId: "adjustModalForm",
                url: ctx + "purchase/check/adjust/edit",
                data: new FormData($("#adjustModalForm")[0]),
                success: function (result) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                    js.modal.hide("modal-adjust");
                },
                error: function (result) {
                    js.modal.fail(result.msg);
                    js.table.refresh(table);
                    js.modal.hide("modal-adjust");
                }
            });
        });


        /**提交盘点损溢调整*/
        $("#btn_submit").on("click", function () {
            js.modal.confirm("尊敬的用户，您确认要提交盘点损溢调整吗？", function () {
                $.post(ctx + 'purchase/check/adjust/submit', {
                    cdlCpnId: [[${checkPlanModel.cpnId}]]
                }, function (result) {
                    if (result.type == 'SUCCESS') {
                        location.href = ctx + "purchase/check";
                    }
                }, 'json')
            });
        });
        /**撤销盘点损溢调整*/
        $("#btn_revoke").on("click", function () {
            js.modal.confirm("尊敬的用户，您确认要撤销盘点损溢调整吗？", function () {
                $.post(ctx + 'purchase/check/adjust/revoke', {
                    cdlCpnId: [[${checkPlanModel.cpnId}]]
                }, function (result) {
                    if (result.type == 'SUCCESS') {
                        js.modal.success(result.msg);
                        location.href = ctx + "purchase/check";
                    }
                    if (result.type == 'ERROR') {
                        js.modal.fail(result.msg);
                    }
                }, 'json')
            });
        });

    });

    /**盘点验证*/
    function formValidator() {
        // 验证初始化
        js.validate.init("adjustModalForm", {
            fields: {
                cdlPalVal: {
                    validators: {
                        notEmpty: {message: '请输入损溢差'}
                    }
                }
            }
        });
    }


    /**
     *@FunctionName: adjust
     *@Description: 调整显示模态框
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 10:35
     *@Phone: 18241927380
     *@Param: [cdlId] 盘点明细主键
     *@Version: 1.0.0
     */
    function adjust(cdlId) {
        $("#adjustModalForm").data('bootstrapValidator').destroy();
        //置空表单验证
        $('#adjustModalForm').data('bootstrapValidator', null);
        //重新绑定表单验证
        formValidator("checkModalForm");
        var rowData = $("#bootstrap-table").bootstrapTable('getRowByUniqueId', cdlId);
        $("#adjustModalForm").fillData(rowData);
        js.modal.open("modal-adjust");
    }
</script>
</body>
</html>
